<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="generator" content="VuePress 2.0.0-beta.38">
    <style>
      :root {
        --c-bg: #fff;
      }
      html.dark {
        --c-bg: #22272e;
      }
      html, body {
        background-color: var(--c-bg);
      }
    </style>
    <script>
      const userMode = localStorage.getItem('vuepress-color-scheme');
			const systemDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
			if (userMode === 'dark' || (userMode !== 'light' && systemDarkMode)) {
				document.documentElement.classList.toggle('dark', true);
			}
    </script>
    <link rel="icon" href="/imgs/favicon.ico"><title>基本配置 | Webpack</title><meta name="description" content="技术型高端人才培训计划系列课程">
    <link rel="modulepreload" href="/webpack5-doc/assets/app.69823687.js"><link rel="modulepreload" href="/webpack5-doc/assets/config.html.902cf69e.js"><link rel="modulepreload" href="/webpack5-doc/assets/config.html.706e9d7a.js">
    <link rel="stylesheet" href="/webpack5-doc/assets/style.d7931ed9.css">
  </head>
  <body>
    <div id="app"><!--[--><div class="theme-container"><!--[--><header class="navbar"><div class="toggle-sidebar-button" title="toggle sidebar" aria-expanded="false" role="button" tabindex="0"><div class="icon" aria-hidden="true"><span></span><span></span><span></span></div></div><span><a href="/webpack5-doc/" class=""><img class="logo" src="/webpack5-doc/imgs/favicon.ico" alt="Webpack"><span class="site-name can-hide">Webpack</span></a></span><div class="navbar-items-wrapper" style=""><!--[--><!--]--><nav class="navbar-items can-hide"><!--[--><div class="navbar-item"><a href="/webpack5-doc/base/" class="router-link-active" aria-label="基础"><!--[--><!--]--> 基础 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/webpack5-doc/progress/" class="" aria-label="进阶"><!--[--><!--]--> 进阶 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/webpack5-doc/senior/" class="" aria-label="高级"><!--[--><!--]--> 高级 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/webpack5-doc/project/" class="" aria-label="项目"><!--[--><!--]--> 项目 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/webpack5-doc/origin/" class="" aria-label="原理"><!--[--><!--]--> 原理 <!--[--><!--]--></a></div><!--]--></nav><!--[--><!--]--><button class="toggle-dark-button" title="toggle dark mode"><svg style="" class="icon" focusable="false" viewBox="0 0 32 32"><path d="M16 12.005a4 4 0 1 1-4 4a4.005 4.005 0 0 1 4-4m0-2a6 6 0 1 0 6 6a6 6 0 0 0-6-6z" fill="currentColor"></path><path d="M5.394 6.813l1.414-1.415l3.506 3.506L8.9 10.318z" fill="currentColor"></path><path d="M2 15.005h5v2H2z" fill="currentColor"></path><path d="M5.394 25.197L8.9 21.691l1.414 1.415l-3.506 3.505z" fill="currentColor"></path><path d="M15 25.005h2v5h-2z" fill="currentColor"></path><path d="M21.687 23.106l1.414-1.415l3.506 3.506l-1.414 1.414z" fill="currentColor"></path><path d="M25 15.005h5v2h-5z" fill="currentColor"></path><path d="M21.687 8.904l3.506-3.506l1.414 1.415l-3.506 3.505z" fill="currentColor"></path><path d="M15 2.005h2v5h-2z" fill="currentColor"></path></svg><svg style="display:none;" class="icon" focusable="false" viewBox="0 0 32 32"><path d="M13.502 5.414a15.075 15.075 0 0 0 11.594 18.194a11.113 11.113 0 0 1-7.975 3.39c-.138 0-.278.005-.418 0a11.094 11.094 0 0 1-3.2-21.584M14.98 3a1.002 1.002 0 0 0-.175.016a13.096 13.096 0 0 0 1.825 25.981c.164.006.328 0 .49 0a13.072 13.072 0 0 0 10.703-5.555a1.01 1.01 0 0 0-.783-1.565A13.08 13.08 0 0 1 15.89 4.38A1.015 1.015 0 0 0 14.98 3z" fill="currentColor"></path></svg></button><form class="search-box" role="search"><input type="search" placeholder="Search" autocomplete="off" spellcheck="false" value><!----></form></div></header><!--]--><div class="sidebar-mask"></div><!--[--><aside class="sidebar"><nav class="navbar-items"><!--[--><div class="navbar-item"><a href="/webpack5-doc/base/" class="router-link-active" aria-label="基础"><!--[--><!--]--> 基础 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/webpack5-doc/progress/" class="" aria-label="进阶"><!--[--><!--]--> 进阶 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/webpack5-doc/senior/" class="" aria-label="高级"><!--[--><!--]--> 高级 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/webpack5-doc/project/" class="" aria-label="项目"><!--[--><!--]--> 项目 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/webpack5-doc/origin/" class="" aria-label="原理"><!--[--><!--]--> 原理 <!--[--><!--]--></a></div><!--]--></nav><!--[--><!--]--><ul class="sidebar-items"><!--[--><li><p tabindex="0" class="sidebar-item sidebar-heading">基础配置 <!----></p><!--[--><ul style="" class="sidebar-item-children"><!--[--><li><a href="/webpack5-doc/base/" class="router-link-active sidebar-item" aria-label="基本配置"><!--[--><!--]--> 基本配置 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/development.html" class="sidebar-item" aria-label="开发模式介绍"><!--[--><!--]--> 开发模式介绍 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/css.html" class="sidebar-item" aria-label="处理样式资源"><!--[--><!--]--> 处理样式资源 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/image.html" class="sidebar-item" aria-label="处理图片资源"><!--[--><!--]--> 处理图片资源 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/output.html" class="sidebar-item" aria-label="修改输出资源的名称和路径"><!--[--><!--]--> 修改输出资源的名称和路径 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/clean.html" class="sidebar-item" aria-label="自动清空上次打包资源"><!--[--><!--]--> 自动清空上次打包资源 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/font.html" class="sidebar-item" aria-label="处理字体图标资源"><!--[--><!--]--> 处理字体图标资源 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/other.html" class="sidebar-item" aria-label="处理其他资源"><!--[--><!--]--> 处理其他资源 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/javascript.html" class="sidebar-item" aria-label="处理 js 资源"><!--[--><!--]--> 处理 js 资源 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/html.html" class="sidebar-item" aria-label="处理 Html 资源"><!--[--><!--]--> 处理 Html 资源 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/server.html" class="sidebar-item" aria-label="开发服务器&amp;自动化"><!--[--><!--]--> 开发服务器&amp;自动化 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/production.html" class="sidebar-item" aria-label="生产模式介绍"><!--[--><!--]--> 生产模式介绍 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/optimizeCss.html" class="sidebar-item" aria-label="Css 处理"><!--[--><!--]--> Css 处理 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/minifyHtml.html" class="sidebar-item" aria-label="html 压缩"><!--[--><!--]--> html 压缩 <!--[--><!--]--></a><!----></li><li><a href="/webpack5-doc/base/summary.html" class="sidebar-item" aria-label="总结"><!--[--><!--]--> 总结 <!--[--><!--]--></a><!----></li><!--]--></ul><!--]--></li><!--]--></ul><!--[--><!--]--></aside><!--]--><!--[--><main class="page"><!--[--><!--]--><div class="theme-default-content"><!--[--><h1 id="基本配置" tabindex="-1"><a class="header-anchor" href="#基本配置" aria-hidden="true">#</a> 基本配置</h1><p>在开始使用 <code>Webpack</code> 之前，我们需要对 <code>Webpack</code> 的配置有一定的认识。</p><h2 id="_5-大核心概念" tabindex="-1"><a class="header-anchor" href="#_5-大核心概念" aria-hidden="true">#</a> 5 大核心概念</h2><ol><li>entry（入口）</li></ol><p>指示 Webpack 从哪个文件开始打包</p><ol start="2"><li>output（输出）</li></ol><p>指示 Webpack 打包完的文件输出到哪里去，如何命名等</p><ol start="3"><li>loader（加载器）</li></ol><p>webpack 本身只能处理 js、json 等资源，其他资源需要借助 loader，Webpack 才能解析</p><ol start="4"><li>plugins（插件）</li></ol><p>扩展 Webpack 的功能</p><ol start="5"><li>mode（模式）</li></ol><p>主要由两种模式：</p><ul><li>开发模式：development</li><li>生产模式：production</li></ul><h2 id="准备-webpack-配置文件" tabindex="-1"><a class="header-anchor" href="#准备-webpack-配置文件" aria-hidden="true">#</a> 准备 Webpack 配置文件</h2><p>在项目根目录下新建文件：<code>webpack.config.js</code></p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code>module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token comment">// 入口</span>
  <span class="token literal-property property">entry</span><span class="token operator">:</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span>
  <span class="token comment">// 输出</span>
  <span class="token literal-property property">output</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token comment">// 加载器</span>
  <span class="token literal-property property">module</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">rules</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token comment">// 插件</span>
  <span class="token literal-property property">plugins</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token comment">// 模式</span>
  <span class="token literal-property property">mode</span><span class="token operator">:</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>Webpack 是基于 Node.js 运行的，所以采用 Common.js 模块化规范</p><h2 id="修改配置文件" tabindex="-1"><a class="header-anchor" href="#修改配置文件" aria-hidden="true">#</a> 修改配置文件</h2><ol><li>配置文件</li></ol><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// Node.js的核心模块，专门用来处理文件路径</span>
<span class="token keyword">const</span> path <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;path&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token comment">// 入口</span>
  <span class="token comment">// 相对路径和绝对路径都行</span>
  <span class="token literal-property property">entry</span><span class="token operator">:</span> <span class="token string">&quot;./src/main.js&quot;</span><span class="token punctuation">,</span>
  <span class="token comment">// 输出</span>
  <span class="token literal-property property">output</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token comment">// path: 文件输出目录，必须是绝对路径</span>
    <span class="token comment">// path.resolve()方法返回一个绝对路径</span>
    <span class="token comment">// __dirname 当前文件的文件夹绝对路径</span>
    <span class="token literal-property property">path</span><span class="token operator">:</span> path<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span> <span class="token string">&quot;dist&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token comment">// filename: 输出文件名</span>
    <span class="token literal-property property">filename</span><span class="token operator">:</span> <span class="token string">&quot;main.js&quot;</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token comment">// 加载器</span>
  <span class="token literal-property property">module</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">rules</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token comment">// 插件</span>
  <span class="token literal-property property">plugins</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token comment">// 模式</span>
  <span class="token literal-property property">mode</span><span class="token operator">:</span> <span class="token string">&quot;development&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 开发模式</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><ol start="2"><li>运行指令</li></ol><div class="language-text ext-text"><pre class="language-text"><code>npx webpack
</code></pre></div><p>此时功能和之前一样，也不能处理样式资源</p><h2 id="小结" tabindex="-1"><a class="header-anchor" href="#小结" aria-hidden="true">#</a> 小结</h2><p>Webpack 将来都通过 <code>webpack.config.js</code> 文件进行配置，来增强 Webpack 的功能</p><p>我们后面会以两个模式来分别搭建 Webpack 的配置，先进行开发模式，再完成生产模式</p><!--]--></div><footer class="page-meta"><!----><!----><!----></footer><!----><!--[--><!--]--></main><!--]--></div><!----><!--]--></div>
    <script type="module" src="/webpack5-doc/assets/app.69823687.js" defer></script>
  </body>
</html>
